import {defineStore} from "pinia";

/**
 * 其内部可以包含，
 * 状态（state），
 * 操作（actions）,
 * getter（getters）
 */
export const index = defineStore("index", {
    state: () => ({
        //左侧导航栏是否折叠
        isCollapse: false,
        //选中菜单信息
        selectedKeys: [1],
        //展开的菜单
        openKeys: <any>[],
        //存储tab标签页的信息
        tagsList: [
            {
                id: 1,
                route: "/home",
                label: "首页",
            },
        ],
    }),
    actions: {
        initStore() {
            const SUBMENULIST: any = JSON.parse(
                // @ts-ignore
                localStorage.getItem("LOGIN_USER")
            ).submenuRespList;
            const CURRENT_MENU: any = localStorage.getItem("CURRENT_MENU");
            if (CURRENT_MENU) {
                //@ts-ignore
                this.selectedKeys = [Number(CURRENT_MENU)];
                for (let i = 0; i < SUBMENULIST.length; i++) {
                    const element = SUBMENULIST[i];
                    if (element.menuEntityList != null) {
                        for (let j = 0; j < element.menuEntityList.length; j++) {
                            const item = element.menuEntityList[j];
                            if (item.id === Number(CURRENT_MENU)) {
                                //@ts-ignore
                                this.openKeys = [element.id];
                                break;
                            }
                        }
                    }
                }
            }
            //@ts-ignore
            const TABS_LIST: any = localStorage.getItem("TABS_LIST");
            if (TABS_LIST) {
                //@ts-ignore
                this.tagsList = JSON.parse(TABS_LIST);
            }
        },
        //collapseMenu 点击头部组件的折叠按钮
        collapseMenu() {
            //@ts-ignore
            this.isCollapse = !this.isCollapse;
        },
        //关闭tab标签页
        closeTab(tag: any) {
            //如果点击的菜单不是home，首先要遍历tabsList是否已经有菜单(name)，如果有删除
            //@ts-ignore
            let result = this.tagsList.findIndex((item) => item.id === tag.id);
            //@ts-ignore
            this.tagsList.splice(result, 1);
            //@ts-ignore
            localStorage.setItem("TABS_LIST", JSON.stringify(this.tagsList));
        },
        //添加tab
        addTab(tag: any) {
            //@ts-ignore
            this.tagsList.forEach((item) => {
                if (item.id !== tag.id) {
                    //@ts-ignore
                    let result = this.tagsList.findIndex((item) => item.id === tag.id);
                    //@ts-ignore
                    result === -1 ? this.tagsList.push(tag) : "";
                }
            });
            //@ts-ignore
            localStorage.setItem("TABS_LIST", JSON.stringify(this.tagsList));
        },
        changeMenu(menu: any) {
            //@ts-ignore
            this.selectedKeys = [menu.id];
            localStorage.setItem("CURRENT_MENU", menu.id);
            const SUBMENULIST: any = JSON.parse(
                // @ts-ignore
                localStorage.getItem("LOGIN_USER")
            ).submenuRespList;
            for (let i = 0; i < SUBMENULIST.length; i++) {
                const element = SUBMENULIST[i];
                if (element.menuEntityList != null) {
                    for (let j = 0; j < element.menuEntityList.length; j++) {
                        const item = element.menuEntityList[j];
                        if (item.id === Number(menu.id)) {
                            //@ts-ignore
                            this.openKeys = [element.id];
                            break;
                        }
                    }
                }
            }
        },
    },
});
